package com.xhtt.modules.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import com.xhtt.modules.task.dao.TaskPotTaskUserMapper;
import com.xhtt.modules.task.entity.*;
import com.xhtt.modules.task.service.TaskPotTaskUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 罐分支任务-分配人员 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-03-05
 */
@Service
public class TaskPotTaskUserServiceImpl extends ServiceImpl<TaskPotTaskUserMapper, TaskPotTaskUser> implements TaskPotTaskUserService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    @Transactional
    @Async
    public void saveShiftDetail(Integer selfId, SuperTaskUser tu, List<TaskPotTask> potTasks) {
        //然后修改人员记录表
        String selfName = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, selfId).select(SysUserEntity::getUserId, SysUserEntity::getName)).getName();
        String targetName = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, tu.getTargetUserId()).select(SysUserEntity::getUserId, SysUserEntity::getName)).getName();

        List<TaskPotTaskUser> newTus = new ArrayList<>();
        TaskPotTaskUser tuser;
        for (TaskPotTask task : potTasks) {
            int count = this.count(new LambdaQueryWrapper<TaskPotTaskUser>().in(TaskPotTaskUser::getPotTaskId, task.getId()));
            if (count == 0)//说明第一次换班，那就添加2条记录
            {
                tuser = new TaskPotTaskUser(task.getId(), selfId, selfName, null, null);
                newTus.add(tuser);
            }
            //不是第一次换班，只要添加这一次人人员记录
            tuser = new TaskPotTaskUser(task.getId(), tu.getTargetUserId(), targetName, tu.getFiles(), tu.getMemo());
            newTus.add(tuser);
        }
        this.saveBatch(newTus);
    }

    @Override
    public void saveShiftDetail(SysUserEntity self, List<TaskPotTask> list) {
        //然后修改人员记录表
        String selfName = self.getName();
        List<TaskPotTaskUser> newTus = new ArrayList<>();
        TaskPotTaskUser tuser;
        for (TaskPotTask task : list) {
            List<TaskPotTaskUser> tus = this.list(new LambdaQueryWrapper<TaskPotTaskUser>().in(TaskPotTaskUser::getPotTaskId, task.getId()));
            if (CollectionUtils.isEmpty(tus))//说明第一次换班，那就添加1条自己的记录
            {
                tuser = new TaskPotTaskUser(task.getId(), self.getUserId().intValue(), selfName, null, null);
                newTus.add(tuser);
            }
        }
        this.saveBatch(newTus);
    }
}
